package com.wdb.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wdb.vo.Picture;

/**
 * @author ld
 * @time 3,15,2015
 * 
 *       DAO:UserDao
 */
public class UserDAO {
	SqlManager manager;
	String sql = "";
	ResultSet rs;

	public UserDAO() throws IOException, ClassNotFoundException, SQLException {
		manager = SqlManager.createInstance();
	}

	/**
	 * 
	 * 
	 * 
	 */

	/**
	 * @param uname
	 * @param password
	 * @return
	 * @throws SQLException
	 */
	public boolean validate(String uname, String password) throws SQLException {
		boolean result = false;
		sql = "select * from user where user_account=? and user_password=?";
		// sql = "insert into user(user_account,user_password) values(?,?)";
		Object[] params = new Object[] { uname, password };
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
		if (rs.next()) {
			result = true;
		}
		manager.closeDB();
		return result;

	}

	/**
	 * @param uname
	 * @param password
	 * @return
	 * @throws SQLException
	 */
	public boolean insertUser(String uname, String password)
			throws SQLException {
		boolean result = false;
		sql = "select * from user where user_account=?";
		// sql2 = "insert into user(user_account,user_password) values(?,?)";
		Object[] params1 = new Object[] { uname };
		manager.connectDB();
		rs = manager.executeQuery(sql, params1);
		if (rs.next() && rs.getLong(1) == 1) {
			result = false;
		} else {
			sql = "insert into user(user_account,user_password) values(?,?)";
			Object[] params2 = new Object[] { uname, password };
			rs = manager.executeQuery(sql, params2);
			result = true;
		}
		manager.closeDB();
		return result;
	}

	public void update(String uname, String password) {
		// throw new NotImplementedException();
	}

	public int findUser(String username, String password) throws SQLException {
		sql = "select * from user where user_account=? and user_password=?";
		// sql = "insert into user(user_account,user_password) values(?,?)";
		Object[] params = new Object[] { username, password };
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
		if (rs.next())
			return rs.getInt(1);
		return -1;
	}

	/**
	 * 新增图片 ttStudio WDB 2015年3月29日
	 */
	public boolean addPic(String pname, String purl, int category, int author,
			String uptime) throws SQLException {
		Object[] params = new Object[] { purl, pname, category, author, uptime };
		sql = "insert into picture(purl,pname,pcategory,pauthor,uptime) values(?,?,?,?,?)";
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
		manager.closeDB();
		return true;
	}

	/**
	 * 获取全部壁纸 ttStudio WDB 2015年3月29日
	 */
	public List<Picture> getAllPic() throws SQLException {
		List<Picture> pics = new ArrayList<Picture>();
		sql = "select * from picture";
		manager.connectDB();
		rs = manager.executeQuery(sql, null);
		if (rs.next()) {
			Picture pic = new Picture();
			pic.setPid(rs.getInt(0));
			pic.setCcount(rs.getInt(6));
			pic.setPauthor(rs.getInt(4));
			pic.setPcategory(rs.getInt(3));
			pic.setPname(rs.getString(2));
			pic.setPurl(rs.getString(1));
			pic.setVcount(rs.getInt(5));
			pic.setUptime(rs.getString(7));
			pics.add(pic);
		}
		manager.closeDB();
		return pics;
	}

	/**
	 * 获取收藏的壁纸列表 ttStudio WDB 2015年3月29日
	 */
	public List<Picture> getMyPictures(int uid) throws SQLException {
		List<Integer> pids = new ArrayList<Integer>();
		List<Picture> pics = new ArrayList<Picture>();
		Object[] params = new Object[] { uid };
		sql = "select * from collect where uid=?";
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
		if (rs.next()) {
			int pid = rs.getInt(1);
			pids.add(pid);
		}

		if (pids != null) {

			sql = "select * from picture where pid = ?";

			for (int i = 0; i < pids.size(); i++) {
				Object[] param2s = new Object[] { pids.get(i) };
				rs = manager.executeQuery(sql, param2s);
				if (rs.next()) {
					Picture pic = new Picture();
					pic.setPid(rs.getInt(0));
					pic.setCcount(rs.getInt(6));
					pic.setPauthor(rs.getInt(4));
					pic.setPcategory(rs.getInt(3));
					pic.setPname(rs.getString(2));
					pic.setPurl(rs.getString(1));
					pic.setVcount(rs.getInt(5));
					pic.setUptime(rs.getString(7));
					pics.add(pic);
				}
			}
		}
		manager.closeDB();
		return pics;
	}

	/**
	 * 用户对图片点赞
	 * ttStudio
	 * WDB
	 * 2015年3月29日
	 */
	public void dianzan(int pid) throws SQLException {
		Object[] params = new Object[] { pid };
		sql = "update collect set dzcount=dzcount + 1 where pid=?";
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
	}
	
	
	/**
	 * 用户浏览
	 * ttStudio
	 * WDB
	 * 2015年3月29日
	 */
	public void viewPic(int pid) throws SQLException {
		Object[] params = new Object[] { pid };
		sql = "update collect set vcount=vcount + 1 where pid=?";
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
	}
	
	
	/**
	 * 用户使用
	 * ttStudio
	 * WDB
	 * 2015年3月29日
	 */
	public void usePic(int pid) throws SQLException {
		Object[] params = new Object[] { pid };
		sql = "update collect set ucount=ucount + 1 where pid=?";
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
	}
	
	
}
